//
//  SearchViewController.m
//  SweetComics
//
//  Created by dlios on 15-1-29.
//  Copyright (c) 2015年 LeeZhe. All rights reserved.
//

#import "SearchViewController.h"
#import "ComicList.h"
#import "DetailViewController.h"
#import "AFHTTPRequestOperationManager.h"
#import "ResultViewController.h"

@interface SearchViewController ()<UISearchBarDelegate>

@property (nonatomic, retain) UISearchBar *searchBar;
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *arr;
@property (nonatomic, retain) NSMutableArray *arr1;
@property (nonatomic, retain) NSMutableArray *arr2;
@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, assign) NSInteger count;

@end

@implementation SearchViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationController.navigationBarHidden = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    self.searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 44)];
    _searchBar.placeholder = @"输入漫画名或作者";
    self.searchBar.delegate = self;
    _searchBar.showsCancelButton = YES;
    _searchBar.showsScopeBar = YES;
    _searchBar.keyboardAppearance = UIKeyboardAppearanceDefault;
    [self.view addSubview:_searchBar];
    [_searchBar release];
    
    self.arr = [NSMutableArray array];
    
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 108, self.view.frame.size.width, self.view.frame.size.height)];
    _scrollView.contentSize = CGSizeMake(0, 800);
    [self.view addSubview:self.scrollView];
    [_scrollView release];
    // 热门搜索
    UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 80, 30)];
    label1.text = @"热门搜索";
    [_scrollView addSubview:label1];
    //    _scrollView.bounces = NO;
    [label1 release];
    
    // 直线
    UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(20, 55, self.view.frame.size.width - 40, 1)];
    label2.backgroundColor =  [UIColor grayColor];
    [_scrollView addSubview:label2];
    [label2 release];
    
    NSString *str = @"http://mobilev3.ac.qq.com/Classify/categoryList/category_id/50007/listcnt/12/page/1/clientType/0";
    NSLog(@"%@", str);
    str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
        NSMutableArray *temp = [NSMutableArray array];
        self.arr = [NSMutableArray array];
        temp = [dic objectForKey:@"data"];
        //        NSLog(@"%@", temp);
        for (NSMutableDictionary *dic1 in temp) {
            ComicList *comic = [[ComicList alloc] init];
            [comic setValuesForKeysWithDictionary:dic1];
            //            NSLog(@"%@", comic);
            [self.arr addObject:comic];
            //            NSLog(@"%@", self.arr);
            [comic release];
        }
        for (NSInteger i = 0; i < self.arr.count; i++) {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
            button.frame = CGRectMake(20 + ((self.view.frame.size.width - 20) / 3) * (i % 3), 75 + 30 * (i / 3), (self.view.frame.size.width - 40) / 3, 20);
            //            [button sizeToFit];
            // 按钮标题左对齐
            button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
            NSString *title = [[[dic objectForKey:@"data"] objectAtIndex:i] objectForKey:@"title"];
            [button setTitle:title forState:UIControlStateNormal];
            //            button.backgroundColor = [UIColor blackColor];
            [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
            [self.scrollView addSubview:button];
        }
        
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
    
}

// 协议方法
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    // 点击按钮 触发协议button方法
    // 回收键盘方法
    //    [searchBar resignFirstResponder];
    [self.navigationController popToRootViewControllerAnimated:NO];
}
// search方法
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [searchBar resignFirstResponder];
    //    [self.view addSubview:_tableView];
    NSString *str = [NSString stringWithFormat:@"http://mobilev3.ac.qq.com/Search/comicSearchListForIos/key_word/%@/listcnt/12/page/1/clientType/0", self.searchBar.text];
    NSLog(@"%@", str);
    str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
        ResultViewController *resultVC = [[ResultViewController alloc] init];
        for (NSMutableDictionary *dic1 in [dic objectForKey:@"data"]) {
            ComicList *comic = [[ComicList alloc] init];
            [comic setValuesForKeysWithDictionary:dic1];
            self.arr1 = [NSMutableArray array];
            [self.arr1 addObject:comic];
            [comic release];
        }
        if (self.arr1) {
            //            resultVC.arr = self.arr1;
            resultVC.Mtitle = self.searchBar.text;
            [self.navigationController pushViewController:resultVC animated:NO];
            [resultVC release];
        }else
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"对不起,没有搜到你要找的漫画" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
            [alert release];
            [resultVC release];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
}

// 每个热门推荐的方法
- (void)buttonAction:(UIButton *)button
{
    self.searchBar.text = button.titleLabel.text;
}

//- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
//{
//    return 1;
//}

//- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//
//}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
